import { getUserInfo } from "@/utils/auth";
import { getStatisticsDropdownList } from "@/components/search-criteria/search-criteria.service";
import { List } from "linqts";

export default {
  name: 'search-criteria',
  components: {},
  props: {
    selectedData: {
      type: Object,
      default: () => {}
    },
    showActionColumn: {
      type: Boolean,
      default: false // 是否显示弹窗
    },
    currentPageKey: {
      type: String,
      default: '' // 是否显示弹窗
    }
  },
  data() {
    return {
      searchCriteria: {
        selectedProvince: '',
        selectedCity: '',
        selectedCountry: '',
        selectedCountrySide: '',
        selectedFarm: '',
        selectedEquipType: '',
        deviceID: '',
        carNum: '',
        carOwner: '',
        startTime: '20180101',
        endTime: '20201231'
      },
      typeList: ['10400', '10401', '10402', '75', '13', '61', '20', '30', '41', '50', '70', '71', '74', '10700'],
      userInfo: {},
      isAdmin: false,
      provinceList: [],
      locations: [],
      cityList: [],
      village: [],
      workAddress: [],
      farms: []
    }
  },
  computed: {},
  created() {
    this.userInfo = getUserInfo();
    // 判断是否是admin
    this.isAdmin = this.userInfo.usertype.toUpperCase() == 'ADMIN' ? true : false;
    this.getSysUserEquipmentAreaList();
  },
  methods: {

    // 获取联动地址
    getSysUserEquipmentAreaList() {
      getStatisticsDropdownList({}).then(data => {
        if (data.StatusCode == 200) {
          this.locations = JSON.parse(data.Entity);
          let list = new List(this.locations);
          this.provinceList =
            list
              .Where(x => this.isAdmin ? true : x.province === this.userInfo.province)
              .Select(x => x.province)
              .Distinct()
              .ToArray();
          // 如果是管理员显示所有得省份，否则只显示对应得地区数据
          if (!this.isAdmin) {
            this.searchCriteria.selectedProvince = this.userInfo.province;
            this.setSelectedProvince();
          }
        }
      });
    },

    // 设置选中的省
    setSelectedProvince() {
      this.searchCriteria.selectedCountry = '';
      this.searchCriteria.selectedCity = '';
      this.searchCriteria.selectedCountrySide = '';
      this.searchCriteria.selectedVillage = '';
      this.searchCriteria.selectedFarm = '';
      if (this.searchCriteria.selectedProvince == '') {
        this.cityList = [];
        this.village = [];
        this.workAddress = [];
        this.farms = [];
      } else {
        let linQList = new List(this.locations);
        this.cityList =
          linQList
            .Where(x => x.province === this.searchCriteria.selectedProvince)
            .Where(x => this.isAdmin ? true : (this.userInfo.city == '' ? true : x.city == this.userInfo.city))
            .Select(x => x.city)
            .Distinct()
            .ToArray();
        // 如果是管理员显示所有得省份，否则只显示对应得地区数据
        if (!this.isAdmin) {
          if (this.userInfo.city != '') {
            this.searchCriteria.selectedCity = this.userInfo.city;
            this.setSelectedCity();
          }
        }
      }
    },

    // 设置选中的城市
    setSelectedCity() {
      this.searchCriteria.selectedCountry = '';
      this.searchCriteria.selectedCountrySide = '';
      this.searchCriteria.selectedVillage = '';
      this.searchCriteria.selectedFarm = '';
      if (this.searchCriteria.selectedCity == '') {
        this.village = [];
        this.workAddress = [];
        this.farms = [];
      } else {
        let linQList = new List(this.locations);
        this.village =
          linQList
            .Where(x => x.city === this.searchCriteria.selectedCity)
            .Where(x => this.isAdmin ? true : (this.userInfo.village == '' ? true : x.village == this.userInfo.village))
            .Select(x => x.village)
            .Distinct()
            .ToArray();
        // 如果是管理员显示所有得省份，否则只显示对应得地区数据
        if (!this.isAdmin) {
          if (this.userInfo.village != '') {
            this.selectedCountry = this.userInfo.village;
            this.setSelectedCountry();
          }
        }
      }
    },

    // 设置选中的区
    setSelectedCountry() {
      this.searchCriteria.selectedCountrySide = '';
      this.searchCriteria.selectedVillage = '';
      this.searchCriteria.selectedFarm = '';
      if (this.searchCriteria.selectedCountry == '') {
        this.workAddress = [];
        this.farms = [];
      } else {
        let linQList = new List(this.locations);
        this.workAddress =
          linQList
            .Where(x => x.village === this.searchCriteria.selectedCountry)
            .Where(x => this.isAdmin ? true : (this.userInfo.prefecture == '' ? true : x.workadress == this.userInfo.prefecture))
            .Select(x => x.workadress)
            .Distinct()
            .ToArray();
        // 如果是管理员显示所有得省份，否则只显示对应得地区数据
        if (!this.isAdmin) {
          if (this.userInfo.prefecture != '') {
            this.searchCriteria.selectedCountrySide = this.userInfo.prefecture;
            this.setSelectedCooperative();
          }
        }
      }
    },

    setSelectedCooperative() {
      this.searchCriteria.selectedFarm = '';
      if (this.searchCriteria.selectedCountrySide == '') {
        this.farms = [];
      } else {
        let linQList = new List(this.locations);
        this.farms =
          linQList
            .Where(x => x.workadress === this.searchCriteria.selectedCountrySide)
            .Select(x => x.cooperativename)
            .Distinct()
            .ToArray();
        if (this.userInfo.usertype == '1') {
          if (this.userInfo.cooperativename != '') {
            this.searchCriteria.selectedFarm = this.userInfo.cooperativename;
          }
        }
      }
    },

    // flag true search, false reset
    search(flag) {
      if (flag == false) {
        this.searchCriteria = {
          selectedProvince: '',
          selectedCity: '',
          selectedCountry: '',
          selectedCountrySide: '',
          selectedFarm: '',
          selectedEquipType: '',
          deviceID: '',
          carNum: '',
          carOwner: '',
          startTime: '20180101',
          endTime: '20201231'
        };
      }

      this.$emit('search', this.searchCriteria);
    },

    exportRecords() {
      this.$emit('export', this.searchCriteria);
    }
  }
}
